package com.liusu.spider.test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liusu.spider.bean.Product;
import com.liusu.spider.request.Request;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

public class PhoneSearch {
    public Request request = new Request();
    @Test
    public void search(){
        String url = "https://search.jd.com/search.php?keyword=%E6%89%8B%E6%9C%BA";

        HashMap<String,String> headers = new HashMap<>();
        headers.put("cookie", "__jdu=16208866839041590938209; shshshfpa=747d3f92-5d40-1cfb-b7ae-03086bbd2fd6-1620886686; shshshfpb=oR0B9IofVeMPuOzRQHTDlNw%3D%3D; __jdv=76161171|direct|-|none|-|1627433763437; areaId=13; ipLoc-djd=13-1007-5505-0; PCSYCityID=CN_370000_370200_370211; __jda=122270672.16208866839041590938209.1620886684.1620886684.1627433763.2; __jdc=122270672; wlfstk_smdl=lwudy5e5vbbln7pcth1pxl90c2ytap3w; thor=FCE4996BDC09D398FC19DAA4E26A45E0FE37D862D71A917861FCE32DD18B19C2FAB0477A30B8BC0C7B684361C5ECC52183F0F2CBB9A7D9A33982F47D1FFCA20245C118A49FA6A1FB732051F0067629922E4FC1C7CDA8C5B87E5E69060EA2463D88DF0C29D0287DA18E0CC2154889E9B876E0112FFB70CEC904FBFC5CF71A102CB8ECD221DB536948EF43661A1F4D5A94; pinId=PlnyWtbK7JWZCgamCypleg; pin=zhangshu4114; unick=zhangshu4114; ceshi3.com=000; _tp=jZjS7SzEi4dGw0V4m65TGA%3D%3D; _pst=zhangshu4114; shshshfp=71c0593c47dd5807c89e81bea99c58ba; rkv=1.0; __jdb=122270672.6.16208866839041590938209|2.1627433763; shshshsID=b59f1b469546eda529c6f0bf2ad02cf3_3_1627433873640; qrsc=3; 3AB9D23F7A4B3C9B=GJNFMR3AZULZTWJC4RSTCTAZEBLVCF3ADNT2F3CSJSWX3GNZLTNDDBNPMR77R5RRVKJJWFIOGQJEZ4TZAOZSWEQI5Q");
        String html = request.get(url, headers);
        Document document = Jsoup.parse(html);

        Elements list_li = document.select("ul[data-tpl] li");
        /**
         * 遍历
         */
        for (Element li : list_li) {

            String spu = li.attr("data-spu");
            /**
             * sku的图片集合
             */
            Elements skus = li.select("div div.p-scroll div.ps-wrap ul li a img");
            for (Element sku : skus) {


                /**
                 * 详情地址
                 *                       [sku]
                 * https://item.jd.com/  100011493273  .html
                 */
                String sku_value = sku.attr("data-sku");
                String picture = "https:"+sku.attr("data-lazy-img");
                String url_item = "https://item.jd.com/"+sku_value+".html";
                /**
                 * 标题
                 */
                String[] list = send_request_get_detail(url_item,headers,sku_value);
                /**
                 * 价格
                 */
                String price = send_request_get_price(headers,sku_value);
                Product product = new Product(spu,sku_value,list[0],price,picture,url_item);
                System.out.println(product);
            }

        }




        System.out.println(html);
    }

    /**
     * 发送请求 获取商品详情数据
     * @param url
     * @param headers
     */
    public String[] send_request_get_detail(String url,HashMap<String,String> headers,String sku){


        String html = request.get(url, headers);
        Document document = Jsoup.parse(html);
        String[] list = new String[10];
        /**
         * 标题
         */
        String sku_name = document.select("div.sku-name").get(0).text();
        list[0] = sku_name;

        /**
         * https://item-soa.jd.com/getWareBusiness?callback=jQuery1206120&skuId=100021830458&cat=9987%2C653%2C655&area=13_1007_5505_0&shopId=1000000904&venderId=1000000904&paramJson=%7B%22platform2%22%3A%221%22%2C%22specialAttrStr%22%3A%22p0pppppppppp1ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1
         */

        return list;
    }

    /**
     * 发送请求 获取商品详情数据
     * @param url
     * @param headers
     * jQuery1206120(
     * jQuery191577(
     */
    public String send_request_get_price(HashMap<String,String> headers,String sku){
        String url="https://item-soa.jd.com/getWareBusiness?callback=jQuery1206120&skuId="+sku+"&cat=9987%2C653%2C655&area=13_1007_5505_0&shopId=1000000904&venderId=1000000904&paramJson=%7B%22platform2%22%3A%221%22%2C%22specialAttrStr%22%3A%22p0pppppppppp1ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1";
        String html = request.get(url, headers);

        int start = html.indexOf("(")+1;
        /**
         * 12345
         * 0   4
         */
        int last = html.length()-1;
        html = html.substring(start,last);
        /**
         * 解析成JSON
         */
        JSONObject json = JSON.parseObject(html);
        Object o = json.getJSONObject("price").get("p");

        return o.toString();
    }
}
